define(["jquery","bbcare","oms_config/root",
        "scprit/followup/app/datatable/datatable.servers.js",
        "scprit/followup/app/datatable/datatable.ui.js",],function ($,$bb,$root,$datatableServers,$datatableUi) {
	
    var servers={};
    var URL = $bb.baseUrl("department")+"followup/service";
    var _URL = $bb.baseUrl("department")+"followup/webapi/service";
    /*
    * 获取运动曲线：
    * */
    servers.getMotionCurData = function(params,callback,otherParams){
        $bb.ajax({
            url: URL + "/nutrition/activities/list",
            data: JSON.stringify({
                data: params
            }),
            success: function (data) {
                if(data.success){
                    callback(data,otherParams);
                }
            }
        });
    };
    /*
     * 获取运动-体操杠铃曲线：
     * */
     servers.getGymnasticBarbellData = function(params,callback,otherParams){
         $bb.ajax({
             url: _URL + "/gymnasticBarbell/listGymnasticBarbell",
             data: JSON.stringify({
                 data: params
             }),
             success: function (data) {
                 if(data.success){
                     callback(data,otherParams);
                 }
             }
         });
     };
     /**
      * 入参
      */
     var inParams = function(args){
         var data = JSON.stringify({
             data: {
                 "args": JSON.stringify(args),
                 "flag": 1,
                 "userId":  $bb.getIocalStorage("patientId"),
                 "startDate":  $("#fromTime").val(),
                 "endDate": $("#endTime").val(),
             }
         });
         return data;
     }
     /**
      * 出参
      */
     var outParams = function(data){
    	 var list = [];
    	 if(data.rows&&data.rows.length){
    		 var dataList = data.rows.sort(function(a,b){return new Date(a.testDate) > new Date(b.testDate)});
    		 $.each(dataList,function(a,b){
    			 if(!list.length){
    				 list.push(b);
    			 }
    			 var flag = true;
    			 $.each(list,function(c,d){
    				 if(d.testDate == b.testDate){
    					 flag = false;
    					 // 时间段,1空腹,2早餐后2小时,3早餐前,4午餐后2小时,5午餐前,6晚餐后2小时,7晚餐前,8其他
    					 switch(b.timeSlot){
    					 case 1:list[c].kf = b.sugarValue;break;
    					 case 2:list[c].zc2 = b.sugarValue;break;
    					 case 3:list[c].zcq = b.sugarValue;break;
    					 case 4:list[c].wh2 = b.sugarValue;break;
    					 case 5:list[c].wucanqian = b.sugarValue;break;
    					 case 6:list[c].wch2 = b.sugarValue;break;
    					 case 7:list[c].wcq = b.sugarValue;break;
    					 case 8:list[c].other = b.sugarValue;break;
    					 }
    				 }
    			 })
    			 if(flag){
    				 switch(b.timeSlot){
    				 case 1:b.kf = b.sugarValue;break;
    				 case 2:b.zc2 = b.sugarValue;break;
    				 case 3:b.zcq = b.sugarValue;break;
    				 case 4:b.wh2 = b.sugarValue;break;
    				 case 5:b.wucanqian = b.sugarValue;break;
    				 case 6:b.wch2 = b.sugarValue;break;
    				 case 7:b.wcq = b.sugarValue;break;
    				 case 8:b.other = b.sugarValue;break;
    				 }
    				 list.push(b);
    			 }
    		 })
    	 }
         return list.sort(function(a,b){return new Date(a.testDate) < new Date(b.testDate)});;
     }
     /**
      * columns
      */
     var columnsData = function(){
         var columnsArr = ['testDate','kf','zc2',"zcq","wh2","wucanqian","wch2","wcq","other"];
         return $datatableServers.getColumnsData(columnsArr);
     }
     /**
      * 行绘制回调函数
      */
     var rowCallback = function(row, data, index) {
    	 console.info(data)
     };
     /**
      * 初始化完成之后的回调函数
      */
     var initComplete = function(setting,json){
    	 $("#bloodsugar").prev("div.row").remove();
         $("#fromTime,#endTime,#self_defined").bind("input propertychange",function(){
        	 $datatableUi.table.api().ajax.reload();  // 出发dt的重新加载数据的方法
         });  
         $("#lastMonth").bind("click",function(){
             var length = $(this).val();
             var args = $datatableUi.table.api().ajax.params();   //获取dt请求参数
             $("#fromTime").val(getBeforeDate(30));
             $("#endTime").val(getBeforeDate(0));
             args = $.extend(args,{"length":length});    // 每页length个
             $datatableUi.table.api().ajax.reload();  // 出发dt的重新加载数据的方法
         });
         /*
          * 数据处理：
          * */
          function getBeforeDate(n){
              var n = n;
              var d = new Date();
              var year = d.getFullYear();
              var mon=d.getMonth()+1;
              var day=d.getDate();
              if(day <= n){
                  if(mon>1) {
                      mon=mon-1;
                  }
                  else {
                      year = year-1;
                      mon = 12;
                  }
              }
              d.setDate(d.getDate()-n);
              year = d.getFullYear();
              mon=d.getMonth()+1;
              day=d.getDate();
              s = year+"-"+(mon<10?('0'+mon):mon)+"-"+(day<10?('0'+day):day);
              return s;
          }
     }
     /**
      * 获取datatable里的options
      */
     servers.getOptions = function(){
         var sessionId = $bb.getSessionStorage("sessionId");
         var options = {
             "ajax": {
                 "url": _URL + "/newnutrition/listbloodsugarForPage?sessionId=" + sessionId,
                 "data": inParams,
                 "dataSrc": outParams
             },
             "columns": columnsData(),
             "rowCallback": rowCallback,
             "initComplete": initComplete,
         };
         return options;
     }
    /*
    * 获取血糖曲线；
    * */
    servers.getBGCurData = function(params,callback,otherParams){
        $bb.ajax({
            url: URL + "/nutrition/bloodsugar/userRecords",
            data: JSON.stringify({
                data: params
            }),
            success: function (data) {
                if(data.success){
                    callback(data,otherParams);
                }
            }
        });
    };
    /*
     * 获取血糖表格；
     * */
     servers.getBGCurTableData = function(params,callback,otherParams){
         $bb.ajax({
             url: _URL + "/newnutrition/listbloodsugarForPage",
             data: JSON.stringify({
                 data: params
             }),
             success: function (data) {
                 if(data.success){
                     callback(data,otherParams);
                 }
             }
         });
     };
    /*
     * 获取睡眠曲线；
     * */
     servers.getSleepData = function(params,callback,otherParams){
         $bb.ajax({
             url: URL + "/nutrition/sleep/list",
             data: JSON.stringify({
                 data: params
             }),
             success: function (data) {
                 if(data.success){
                     callback(data,otherParams);
                 }
             }
         });
     };
     /*
      * 获取体重曲线；
      * */
      servers.getWeightData = function(params,callback,otherParams){
          $bb.ajax({
              url: _URL + "/weight/listWeightByIdAndDate",
              data: JSON.stringify({
                  data: params
              }),
              success: function (data) {
                  if(data.success){
                      callback(data,otherParams);
                  }
              }
          });
      };
    /*
     * 设备绑定：
     * */
    servers.equipBind = function(params,callback,otherParams){
        $bb.ajax({
            url: URL + "/nutrition/device/binding",
            data: JSON.stringify({
                data: params
            }),
            success: function (data) {
                if(data.success){
                    callback(data,otherParams);
                }else{
                    $bb.toastShow(data.msg,1500);
                }
            }
        });
    };
    /*
     * 设备解绑：
     * */
    servers.equipUnbind = function(params,callback,otherParams){
        $bb.ajax({
            url: URL + "/nutrition/device/unbind",
            data: JSON.stringify({
                data: params
            }),
            success: function (data) {
                if(data.success){
                    callback(data,otherParams);
                }else{
                    $bb.toastShow(data.msg,1500);
                }
            }
        });
    };
    /*
     * 华米授权：
     * */
    servers.authority = function(params,callback,otherParams){
        $bb.ajax({
            url: URL + "/nutrition/authorization/geturl",
            data: JSON.stringify({
                data: params
            }),
            success: function (data) {
                if(data.success){
                    callback(data,otherParams);
                }else{
                    $bb.toastShow(data.msg);
                }
            }
        });
    };
    /*
    * 删除患者：
    * */
    servers.deletePatient = function(params){
        $bb.ajax({
            url: URL + "/patient/history/patientDel",
            data: JSON.stringify({
                data: params
            }),
            success: function (data) {
                params.callback(data);
            }
        });
    };
    /*
     * 获取随访任务id;
     * */
    servers.getTaskId = function(params,callback,otherParams){
        $bb.ajax({
            url: URL + "/patient/patientTaskListQuery",
            data:JSON.stringify({
                data : params
            }),
            success: function(data){
                if(data.success){
                    callback(data,otherParams);
                }
            }
        });
    };
    /*
     * 获取建档问卷和内容
     * */
    servers.getRecordQuestions = function(params,callback,otherParams){
        $bb.ajax({
            url: URL + "/patient/patientTaskContentDataQuery",
            data:JSON.stringify({
                data : params
            }),
            success: function(data){
                if(data.success){
                    callback(data,otherParams);
                }
            }
        });
    };

    return servers;
});